/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
 var topKFrequent = function(nums, k) {
    let map = new Map()
    for(let i of nums){
      if(map.get(i)){
        map.set(i,map.get(i)+1)
      }else{
        map.set(i,1)
      }
    }
    if(map.size < k){
      return [...map.keys()]
    }
    console.log(map);
    return bucketSort(map,k)
};
function bucketSort(map,k){
  let arr = [], res = []
  map.forEach((value,key)=>{
    console.log('value:',value,'key:',key);
    if(!arr[value]){
      arr[value] = [key]
    }else{
      arr[value].push(key)
    }
  })
  console.log(arr);
  for(let i = arr.length - 1;i >= 0 && res.length < k;i--){
    if(arr[i]){
      res.push(...arr[i])
    }
  }
  return res
}
console.log(topKFrequent([1,1,1,2,2,3],2));